package com.crazypos.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazypos.common.ApiRestResponse;
import com.crazypos.common.utils.SecurityUtil;
import com.crazypos.pojo.OrganizationEntity;
import com.crazypos.pojo.bo.AdminCreateRootOrganizationBO;
import com.crazypos.pojo.bo.AdminUpdateOrganizationBO;
import com.crazypos.service.IOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/organization")
public class AdminOrganizationController {

    @Autowired
    private IOrganizationService organizationService;

    @GetMapping("list")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse getOrganizationList(
            @RequestParam(value = "1", required = false) Integer startPage,
            @RequestParam(value = "10", required = false) Integer pageSize,
            @RequestParam(required = false) String keyworkd
    ) {
        IPage<OrganizationEntity> result = organizationService.getOrganizationList(startPage, pageSize, keyworkd);
        return ApiRestResponse.success(result);
    }

    /***
     * 创建最顶层组织
     * @return
     */
    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse createRootOrganization(@RequestBody @Validated AdminCreateRootOrganizationBO createRootOrganizationBO) {
        String currentUserId = SecurityUtil.getUserId();

        organizationService.adminCreateOrganization(createRootOrganizationBO, currentUserId);
        return ApiRestResponse.success();
    }

    /***
     * 更新最顶层组织
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse updateRootOrganization(@RequestBody @Validated AdminUpdateOrganizationBO bo) {
        organizationService.adminUpdateOrganization(bo);
        return ApiRestResponse.success();
    }
}
